<!--
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements.  See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership.  The ASF licenses this file
 to you under the Apache License, Version 2.0 (the
 "License"); you may not use this file except in compliance
 with the License.  You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.
-->

<script lang="ts">
  import { createBubbler } from 'svelte/legacy';

  const bubble = createBubbler();
  import type { HTMLInputTypeAttribute } from 'svelte/elements';
  import { twMerge } from 'tailwind-merge';
  import { v4 as uuidv4 } from 'uuid';

  interface Props {
    errorMessage?: string;
    id?: string;
    label?: string;
    name: string;
    options: Array<string>;
    type?: HTMLInputTypeAttribute;
    value: string | number;
    prefix?: import('svelte').Snippet;
    suffix?: import('svelte').Snippet;
    [key: string]: any;
  }

  let {
    errorMessage = undefined,
    id = uuidv4(),
    label = undefined,
    name,
    options,
    value = $bindable(),
    prefix,
    suffix,
    ...rest
  }: Props = $props();

  const inputProps = {
    class: twMerge(
      'w-full px-4 h-full rounded-lg outline-hidden bg-transparent text-color',
      prefix && 'pl-9',
      suffix && 'pr-12'
    ),
    id,
    name,
    ...rest
  };
</script>

<div class="flex flex-col gap-2">
  {#if label}
    <label for={id} class="ml-1 text-sm text-color">
      {label}
    </label>
  {/if}

  <div
    class={twMerge(
      'rounded-md dark:bg-shade-d400  ring-1 ring-gray-300 dark:ring-gray-500 flex items-center h-[40px] text-color relative focus-within:ring-2 focus-within:ring-gray-400 transition group',
      errorMessage && 'ring-red-600! ring-2 '
    )}
  >
    {#if prefix}
      <div class="absolute flex items-center justify-center -translate-y-1/2 left-2 top-1/2">
        {@render prefix?.()}
      </div>
    {/if}

    <select bind:value oninput={bubble('input')} {...inputProps}>
      {#each options as option (option)}
        <option class="dark:bg-shade-d400 dark:text-white bg-white text-black">{option}</option>
      {/each}
    </select>

    {#if suffix}
      <div class="absolute flex items-center justify-center -translate-y-1/2 right-2 top-1/2">
        {@render suffix?.()}
      </div>
    {/if}
  </div>

  {#if errorMessage}
    <span class="ml-1 text-xs font-medium text-red-600">
      {errorMessage}
    </span>
  {/if}
</div>
